package net.chromedpork.scraper.configuration;



import java.util.Collections;
import java.util.Map;

import net.chromedpork.hypertext.validation.Assertion;



/**
 *
 *  Scraper configuration object.
 * 
 *  @author Inode <inode@chromedpork.net>
 *
 */

public class Configuration
{
    private final Map<String, String> request;
    private final Map<String, String> headers;
    private final Map<String, String> data;
    private final Map<String, String> filter;
    private final Map<String, String> expression;


    /**
     *
     *  Constructs a configuration object from passed arguments.
     *
     *  @param request      HTTP request (method + URL) map
     *  @param headers      HTTP request header definition map
     *  @param data         HTTP request payload definition map
     *  @param filter       HTTP response payload Filter definition map
     *  @param expression   HTTP response payload Filter expression map
     *
     */

    Configuration(Map<String, String> request,
                  Map<String, String> headers,
                  Map<String, String> data,
                  Map<String, String> filter,
                  Map<String, String> expression)
    {
        Assertion.notNull(request, "request configuration is null");
        Assertion.notNull(headers, "header configuration is null");
        Assertion.notNull(data, "data configuration is null");
        Assertion.notNull(filter, "filter configuration is null");
        Assertion.notNull(expression, "expression configuration is null");

        this.request    = Collections.unmodifiableMap(request);
        this.headers    = Collections.unmodifiableMap(headers);
        this.data       = Collections.unmodifiableMap(data);
        this.filter     = Collections.unmodifiableMap(filter);
        this.expression = Collections.unmodifiableMap(expression);
    }


    /**
     *
     *  Obtains HTTP request (method, URL) map
     *
     *  @return Map containing method and URL keys and values
     *
     */

    public Map<String, String> getRequest()
    {
        return (request);
    }


    /**
     *
     *  Fetches the HTTP request method.
     *
     *  @return request method
     *
     */

    public String getRequestMethod()
    {
        return (request.get("method"));
    }
    

    /**
     *
     *  Fetches the HTTP request URL.
     *
     *  @return request URL
     *
     */

    public String getRequestURL()
    {
        return (request.get("url"));
    }


    /**
     *
     *  Obtains HTTP request header map.
     *
     *  @return Map containing header keys and values
     *
     */
    
    public Map<String, String> getHeaders()
    {
        return (headers);
    }


    /**
     *
     *  Fetches the HTTP request headers as a single string.
     *
     *  @return request header string
     *
     */
    
    public String getHeadersString()
    {
        String headersString = "";

        for (String headerKey : data.keySet())
        {
            headersString += headerKey + ": " + headers.get(headerKey) + "\r\n";
        }

        return (headersString);
    }


    /**
     *
     *  Obtains HTTP request payload/data variable map.
     *
     *  @return Map containing keys and values for request payload variables
     *
     */
    
    public Map<String, String> getData()
    {
        return (data);
    }


    /**
     *
     *  Fetches the HTTP request payload/data variables as a single string.
     *
     *  @return request payload string
     *
     */
    
    public String getDataString()
    {
        String dataString = "";

        for (String dataKey : data.keySet())
        {
            dataString += dataKey + "=" + data.get(dataKey) + "&";
        }

        return (dataString);
    }


    /**
     *
     *  Obtains HTTP response filter definition map.
     *
     *  @return Map containing keys and values for request filter definition
     *
     */
    
    public Map<String, String> getFilter()
    {
        return (filter);
    }


    /**
     *
     *  Fetches the HTTP response payload filter type.
     *
     *  @return response payload filter type
     *
     */
    
    public String getFilterType()
    {
        return (filter.get("type"));
    }


    /**
     *
     *  Obtains HTTP response filter expression map.
     *
     *  @return Map containing keys and values for request filter expression
     *
     */
    
    public Map<String, String> getExpression()
    {
        return (expression);
    }


    /**
     *
     *  Fetches the HTTP response payload filter expression key.
     *
     *  @return response payload filter expression key
     *
     */
    
    public String getExpressionKey()
    {
        return (expression.get("key"));
    }


    /**
     *
     *  Fetches the HTTP response payload filter expression value.
     *
     *  @return response payload filter expression value
     *
     */

    public String getExpressionValue()
    {
        return (expression.get("value"));
    }


    /**
     *
     *  Fetches the HTTP response payload filter expression return attribute.
     *
     *  @return response payload filter expression return attribute
     *
     */
    
    public String getExpressionReturn()
    {
        return (expression.get("return"));
    }
}
